using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UpSceneDoorTrigger : TriggerBase
{
    [SerializeField] private Transform _reStartTrans;
    [SerializeField] private int _weaponSelectCount = 2;
    [SerializeField] private WeaponScrollDataListSO _weaponScrollDataListSO;
    [SerializeField] private GameObject zujian;

    private DialogueDataBase _selectDialogueData;
    private List<WeaponScrollDataSO> _selectedWepaon = new();
    private UpSceneDoorManager _upSceneDoorManager;
    public bool CanGetWeaponScroll { get; private set; }
    private bool _canInteract;
    protected override void Awake()
    {
        base.Awake();
        _upSceneDoorManager = GetComponentInParent<UpSceneDoorManager>();
        _canInteract = true;
    }
    
    protected override void OnTriggerEnter2D(Collider2D other)
    {
        if (other.CompareTag("Player") == false || _canInteract == false)
            return;
        PlayerInventory playerInventory = other.GetComponent<PlayerInventory>();

        if (CanGetWeaponScroll)
        {
            EventCenter.TriggerEvent<DialogueDataBase, Action>(EventName.UI_OpenDialoguePanel, _selectDialogueData, () =>
            {
                _selectedWepaon = playerInventory.GetCanSelectWeapon(_weaponScrollDataListSO, _weaponSelectCount);
                EventCenter.TriggerEvent(EventName.UI_OpenWeaponSelectPanel, _selectedWepaon);

                playerInventory.AddInventoryItem(zujian.GetComponent<ConpentnentTrigger>()._itemDataSO);
                
                _upSceneDoorManager.SetGameSuccess();
            });
        }
        else
        {
            EventCenter.TriggerEvent<DialogueDataBase, Action>(EventName.UI_OpenDialoguePanel, _selectDialogueData, () =>
            {
                EventCenter.TriggerEvent<float, Action>(EventName.PlayerSlectFail, .5f, () =>
                {
                    Player.Instance.transform.position = _reStartTrans.position;
                    _upSceneDoorManager.ResetAllTrigger();
                });
            });
        }

    }
    public void SetupTriggerCanGetScroll(DialogueDataBase selectFailDialogueData, WeaponScrollDataListSO weaponScrollDataListSO)
    {
        _selectDialogueData = selectFailDialogueData;
        _weaponScrollDataListSO = weaponScrollDataListSO;

        CanGetWeaponScroll = true;
    }

    public void ResetTrigger(DialogueDataBase selectDialogueData)
    {
        CanGetWeaponScroll = false;
        _selectDialogueData = null;
        _weaponScrollDataListSO = null;

        _selectDialogueData = selectDialogueData;
    }
    public void DiabaleInteract() => _canInteract = false;
}
